<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="姓名" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.mobile" placeholder="手机号码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleCurrentChange(1)">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table
      v-loading="listLoading"
      :data="items"
      highlight-current-row
      style="width: 100%;"
      @selection-change="selsChange"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="60" />
      <el-table-column prop="userName" label="姓名" sortable="custom" />
      <el-table-column prop="loginName" label="登录名" sortable="custom" />
      <el-table-column prop="email" label="电子邮件" sortable="custom" />
      <el-table-column prop="mobile" label="手机号码" sortable="custom" />
      <el-table-column prop="roleName" label="角色" sortable="custom" />
      <el-table-column prop="isActive" label="是否激活" sortable="custom">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="Tool.statusFilter(scope.row.isActive)">{{ scope.row.isActiveName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isAdmin" label="是否管理员" sortable="custom">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="Tool.adminFilter(scope.row)">{{ scope.row.isAdminName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit-outline"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="handleDel(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="sels.length === 0" @click="batchRemove">批量删除</el-button>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :current-page.sync="page"
        :page-size="10"
        :total="total"
        style="float:right;"
        @current-change="handleCurrentChange"
      />
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="dbFormVisible" :close-on-click-modal="false">
      <el-form ref="dbForm" :model="dbForm" label-width="100px" :rules="dbFormRules">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="dbForm.userName" auto-complete="off" />
        </el-form-item>
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="dbForm.loginName" auto-complete="off" />
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="dbForm.email" auto-complete="off" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="dbForm.mobile" auto-complete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="passwd">
          <el-input
            v-model="dbForm.passwd"
            auto-complete="off"
            type="password"
            placeholder="新增时必须填写，更新信息时不更改请留空"
          />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="dbForm.roleId" placeholder="请选择角色">
            <el-option
              v-for="role in menus.roles"
              :key="role.roleId"
              :label="role.roleName"
              :value="role.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否激活">
          <el-radio-group v-model="dbForm.isActive">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否管理员">
          <el-radio-group v-model="dbForm.isAdmin">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dbFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="dbLoading" @click.native="dbSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import Tool from '@/utils/tool'
import Api from '@/api/api'
import qs from 'qs'

export default {
  data() {
    return {
      Tool,
      pageController: 'user',
      filters: {
        name: '',
        mobile: ''
      },

      menus: {
        roles: []
      },

      items: [],
      total: 0,
      page: 1,
      order: '',
      sortby: '',
      listLoading: false,
      sels: [], // 列表选中列

      dbFormVisible: false, // 编辑界面是否显示
      dbMethod: '', // 是更新还是新增操作
      dbLoading: false,
      companyLoading: false,
      actorLoading: false,

      dbFormRules: {
        userName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        loginName: [{ required: true, message: '请输入登录名', trigger: 'blur' }],
        email: [{ type: 'email', required: false, message: '请输入正确的邮箱地址', trigger: 'blur' }]
      },
      // 编辑界面数据
      dbForm: {
        userId: 0,
        userName: '',
        loginName: '',
        email: '',
        mobile: '',
        passwd: '',
        roleId: 0,
        isActive: 0,
        isAdmin: 0
      }
    }
  },
  mounted() {
    this.getList()
    this.getMenus()
  },
  methods: {
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    // 获取用户列表
    getList() {
      const vue = this
      Tool.getList(vue, { userName: vue.filters.name, userMobile: vue.filters.mobile })
    },
    getMenus() {
      const vue = this
      Tool.getMenus(vue)
    },
    // 删除
    handleDel: function(index, row) {
      const vue = this
      Tool.handleDel(vue, { id: row[this.pageController + 'Id'] })
    },
    // 显示编辑界面
    handleEdit: function(index, row) {
      this.dbFormVisible = true
      this.dbMethod = 'update'
      row.passwd = ''
      if (!row.roleId) {
        row.roleId = ''
      }
      this.dbForm = Object.assign({}, row)
    },
    // 显示新增界面
    handleAdd: function() {
      this.dbFormVisible = true
      this.dbMethod = 'create'
      this.dbForm = {
        userId: 0,
        userName: '',
        loginName: '',
        email: '',
        mobile: '',
        isActive: 0,
        isAdmin: 0
      }
    },
    // 编辑
    dbSubmit: function() {
      const vue = this
      Tool.dbSubmit(vue)
    },
    dbSuccess() {
      const vue = this
      Tool.dbSuccess(vue)
    },
    selsChange: function(sels) {
      this.sels = sels
    },
    sortChange(column) {
      const vue = this
      Tool.sortChange(vue, column)
    },
    // 批量删除
    batchRemove: function() {
      const vue = this
      const ids = vue.sels.map(item => item[this.pageController + 'Id']).toString()
      Tool.batchRemove(vue, ids)
    }
  }
}
</script>

<style scoped></style>
